﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAL;
using Model;
namespace UI.system.page.product
{
    public partial class productparam : BaseAdmin
    {
        public int category1id = 0;
        public int category2id = 0;
        public int productid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            productid = JobRequest.GetRequestInt("productid");
            category1id = JobRequest.GetRequestInt("category1id");
            category2id = JobRequest.GetRequestInt("category2id");
            int adminid = B.MyAdmin.ID;
            if (productid == 0 && JobRequest.GetRequestInt("setparam")==0)
            {
                ProductParamDAL.DeleteByProductID(adminid);
                List<ParamInfo> paramlist = ParamDAL.FindByCategory(adminid, category1id, category2id);
                foreach (ParamInfo info in paramlist.Where(x => x.ParentID == 0))
                {
                    List<ParamInfo> childlist = paramlist.Where(x => x.ParentID == info.ID).ToList();
                    ProductParamInfo productparam = new ProductParamInfo()
                    {
                        AdminID = adminid,
                        ProductID = productid,
                        ParentID = 0,
                        Name = info.Name,
                        Value = "",
                        Sort = info.Sort,
                        Type = info.Type
                    };
                    int ID = ProductParamDAL.Save(productparam);
                    foreach (ParamInfo child in childlist)
                    {
                        ProductParamInfo pp = new ProductParamInfo()
                        {
                            AdminID = adminid,
                            ProductID = productid,
                            ParentID = ID,
                            Name = child.Name,
                            Value = "",
                            Sort = child.Sort,
                            Type = child.Type
                        };
                        ProductParamDAL.Save(pp);
                    }
                }
            }
        }
    }
}